package njust.edu.controller;

import com.google.gson.Gson;
import njust.edu.entity.Dish;
import njust.edu.entity.Trade;
import njust.edu.entity.TradeItem;
import njust.edu.entity.User;
import njust.edu.exception.DishStockNotEnoughException;
import njust.edu.service.DishService;
import njust.edu.service.TradeService;
import njust.edu.service.impl.DishServiceImpl;
import njust.edu.service.impl.TradeServiceImpl;
import njust.edu.web.Cart;
import njust.edu.web.CartItem;
import njust.edu.web.DateSale;
import njust.edu.web.Page;

import javax.jws.soap.SOAPBinding;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


@WebServlet(name = "TradeController", value = "/TradeController")
public class TradeController extends HttpServlet {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    //根据参数“id” 修改订单状态
//    public void changeState(HttpServletRequest request,
//                            HttpServletResponse response) throws ServletException, IOException {
//        PrintWriter out = response.getWriter();
//        String result = "0";
//        String idStr = request.getParameter("id");
//        Long id = 1l;
//        try {
//            id = Long.parseLong(idStr);
//        } catch (NumberFormatException e) {
//        }
//
//        String nowState = tradeService.changeState(id);
//
//        // result = String.valueOf(nowState);
//        if (nowState != null) {
//            result = nowState;
//        }
//        out.write(result);
//        out.close();
//    }

    //根据参数"tradeId" "to_flag" "pageNum"获取Trade和Cart
    public void getTrade(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        String tradeIdStr = request.getParameter("tradeId");
        String to_flag = request.getParameter("to_flag");
        Long tradeId = 201500003l;
        try {
            tradeId = Long.parseLong(tradeIdStr);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String pageNum = request.getParameter("pageNum");
        Trade trade = tradeService.getTrade(tradeId);
        Cart cart = tradeService.getTradeInfoToCart(tradeId);
        if (to_flag != null) {
            request.setAttribute("to_flag", to_flag);
        }
        request.setAttribute("trade", trade);
        request.setAttribute("cart", cart);
        request.getRequestDispatcher(
                "WEB-INF/jsp/trade/trade-info.jsp?pageNum=" + pageNum).forward(
                request, response);
    }

    //若购物车为空进入购物车页面  若不为空则下单进入订单页面并减少相对应的库存
    public void addTrade(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException, DishStockNotEnoughException {
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        User user = (User) session.getAttribute("user");
        String tableStr = request.getParameter("tableNum");
        Integer tableId = 1101;
        if(tableStr!=null){
                tableId = Integer.parseInt(tableStr);
        }
        tradeService.addTradeList(cart, user.getId(), tableId);
        cart.clear();
        response.sendRedirect("TradeController?method=toTradeAllUI");

    }

    TradeService tradeService = new TradeServiceImpl();
    DishService dishService = new DishServiceImpl();

    //显示所有状态小于参数“state”的订单 显示第“pageNo”页信息
    public void toTradeUI(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        String pageNoStr = request.getParameter("pageNo");
        String stateStr = request.getParameter("state");
        int pageNo = 1;
        int state = 4;
        try {
            pageNo = Integer.parseInt(pageNoStr);
            state = Integer.parseInt(stateStr);
        } catch (NumberFormatException e) {
            // e.printStackTrace();
        }
        Page<Trade> page = tradeService.getPage(pageNo, state);
        request.setAttribute("page", page);

        request.getRequestDispatcher("WEB-INF/jsp/trade/tradeAll.jsp")
                .forward(request, response);
    }

    //本月业绩
    public void getTimeTrade(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        List<DateSale> list =tradeService.getMSales();
        PrintWriter out = response.getWriter();

        Gson gson = new Gson();
        String result = "";
        result = gson.toJson(list);
        out.print(result);

        out.close();

    }

    //当日业绩 Todo
    public void getRecentTrade(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        List<DateSale> list =tradeService.getDSales();

//        List<DateSale> list= new ArrayList<DateSale>();
//        for (double i = 0; i <= 6; i++) {
//            DateSale dateSale = new DateSale();
//            dateSale.setDate("2021/9/25");
//            dateSale.setMoney(i);
//
//            list.add(dateSale);
//        }

        PrintWriter out = response.getWriter();


        Gson gson = new Gson();
        String result = "";
        result = gson.toJson(list);

        out.write(result);


        out.close();

    }

    //客户显示订单
    public void toTradeAllUI(HttpServletRequest request,
                             HttpServletResponse response) throws ServletException, IOException {
        String pageNoStr = request.getParameter("pageNo");
        String stateStr = request.getParameter("state");
        int pageNo = 1;
        int state = 5;
        if (pageNoStr!=null){
            try {
                pageNo = Integer.parseInt(pageNoStr);
            } catch (NumberFormatException e) {
            }
        }
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        request.setAttribute("user",user);
        Page<Trade> page = tradeService.getPage(pageNo, state);
        request.setAttribute("page", page);
        request.getRequestDispatcher("WEB-INF/jsp/trade/tradeAll.jsp").forward(
                request, response);
    }

    //员工显示订单
    public void toAcceptOrderUI(HttpServletRequest request,
                             HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        request.setAttribute("user",user);
        String pageNoStr = request.getParameter("pageNo");
        String stateStr = request.getParameter("state");
        int pageNo = 1;
        int state = 2;
        if (pageNoStr!=null){
            try {
                pageNo = Integer.parseInt(pageNoStr);
            } catch (NumberFormatException e) {
            }
        }
        Page<Trade> page = tradeService.getPage(pageNo, state);
        request.setAttribute("page", page);
        request.getRequestDispatcher("WEB-INF/jsp/trade/accept_order.jsp").forward(
                request, response);
    }

    //员工接收订单
    public void acceptOrder(HttpServletRequest request,
                                HttpServletResponse response) throws ServletException, IOException {
        String tradeIdStr = request.getParameter("tradeId");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        request.setAttribute("user",user);
        Long tradeId = Long.parseLong(tradeIdStr);
//        tradeService.setStaffId(tradeId,user.getId());
        tradeService.changeState(tradeId, user.getId());
        request.getRequestDispatcher("TradeController?method=toAcceptOrderUI").forward(
                request, response);
    }

    //查询某个订单的具体信息
    public void tradeInfo(HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String tradeIdStr = request.getParameter("tradeId");
        Long tradeId = Long.parseLong(tradeIdStr);
        List<TradeItem> tradeItemList = tradeService.getTradeItemList(tradeId);
        Trade trade = tradeService.getTrade(tradeId);
        request.setAttribute("trade",trade);
        request.setAttribute("tradeItemList",tradeItemList);
        request.getRequestDispatcher("WEB-INF/jsp/trade/trade_info.jsp").forward(
                request, response);
    }

    public void tradeManageInfo(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        String tradeIdStr = request.getParameter("tradeId");
        Long tradeId = Long.parseLong(tradeIdStr);
        List<TradeItem> tradeItemList = tradeService.getTradeItemList(tradeId);
        Trade trade = tradeService.getTrade(tradeId);
        request.setAttribute("trade",trade);
        request.setAttribute("tradeItemList",tradeItemList);
        request.getRequestDispatcher("WEB-INF/jsp/trade/trade_manage_info.jsp").forward(
                request, response);
    }

    //管理员显示订单
    public void toTradeManageUI(HttpServletRequest request,
                             HttpServletResponse response) throws ServletException, IOException {
        String pageNoStr = request.getParameter("pageNo");
        String stateStr = request.getParameter("state");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        request.setAttribute("user",user);
        int pageNo = 1;
        int state = 5;
        if (pageNoStr!=null){
            try {
                pageNo = Integer.parseInt(pageNoStr);
            } catch (NumberFormatException e) {
            }
        }
        if (stateStr!=null){
            try {
                state = Integer.parseInt(stateStr);
            } catch (NumberFormatException e) {
            }
        }
        Page<Trade> page = tradeService.getPage(pageNo, state);
        request.setAttribute("page", page);
        request.getRequestDispatcher("WEB-INF/jsp/trade/tradeManage.jsp").forward(
                request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String methodName = request.getParameter("method");
        try {
            Method method = getClass().getDeclaredMethod(methodName,
                    HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            method.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}

